// Cox-regression analysis for five years

drop if Gender ==.

recode ITU_death_days (min/1825.1=1), gen(event2)
replace event2 = 0 if event2 ==.
replace event2 = 0 if event2 >1825.1
recode Days_alive (1825.1/max=1825), gen(TTE)
drop if TTE<0
stset TTE, failure (event2==1)

tabulate event time_to_event
by event, sort: summ Days_alive

stcox Age
estat phtest, detail
stcox ib2.Gender
estat phtest, detail
stcox i.FCS
estat phtest, detail
stcox ib2.Med_Surg_Diag
estat phtest, detail
stcox LSOA
estat phtest, detail
stcox IDAOPI
estat phtest, detail
stcox i.Readmission_within_hosp_ad
estat phtest, detail


stcox PFratio
estat phtest, detail
stcox Lowest_SBP
estat phtest, detail
stcox Lowest_pH
estat phtest, detail
stcox Lactate
estat phtest, detail
stcox i.Lowest_GCS
estat phtest, detail
stcox Lowest_platelet
estat phtest, detail
stcox Highest_creatinine
estat phtest, detail
stcox Highest_bilirubin
estat phtest, detail

stcox 
estat phtest, detail

// Violated PH assumprions by Gender, Lowest_SBP and Lactate 

// Flexible parametric survival model

stpm2 Lowest_SBP, df(3) scale(hazard) eform
estat ic
stpm2 Lowest_SBP, df(4) scale(hazard) eform
estat ic
stpm2 Lowest_SBP, df(5) scale(hazard) eform
estat ic

stpm2 i.Gender, df(3) scale(hazard) eform
estat ic
stpm2 i.Gender, df(4) scale(hazard) eform
estat ic
stpm2 i.Gender, df(5) scale(hazard) eform
estat ic

stpm2 Lactate, df(3) scale(hazard) eform
estat ic
stpm2 Lactate, df(4) scale(hazard) eform
estat ic
stpm2 Lactate, df(5) scale(hazard) eform
estat ic

stpm2 PFratio, df(3) scale(hazard) eform
estat ic
stpm2 PFratio, df(4) scale(hazard) eform
estat ic
stpm2 PFratio, df(5) scale(hazard) eform
estat ic

stpm2 Lowest_pH, df(3) scale(hazard) eform
estat ic
stpm2 Lowest_pH, df(4) scale(hazard) eform
estat ic
stpm2 Lowest_pH, df(5) scale(hazard) eform
estat ic


//More splines than this "not feasible in STATA"
// df5 chosen as most parsimonious model for each of the three variables

stpm2 Age, df(5) scale(hazard) eform
stpm2 ib2.Gender, df(5) scale(hazard) eform
stpm2 i.FCS, df(5) scale(hazard) eform
stpm2 ib2.Med_Surg_Diag, df(5) scale(hazard) eform
stpm2 LSOA, df(5) scale(hazard) eform
stpm2 i.Readmission_within_hosp_ad, df(5) scale(hazard) eform
stpm2 Lowest_SBP, df(5) scale(hazard) eform
stpm2 Lowest_pH, df(5) scale(hazard) eform
stpm2 Lactate, df(5) scale(hazard) eform
stpm2 ib1.GCSbin, df(5) scale(hazard) eform
stpm2 Lowest_platelet, df(5) scale(hazard) eform
stpm2 Highest_creatinine, df(5) scale(hazard) eform
stpm2 Highest_bilirubin, df(5) scale(hazard) eform
stpm2 PFratio, df(5) scale(hazard) eform


stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio Lowest_SBP Lowest_pH Lactate ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(3) scale(hazard) eform
estat ic
stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio Lowest_SBP Lowest_pH Lactate ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(4) scale(hazard) eform
estat ic
stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio Lowest_SBP Lowest_pH Lactate ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(5) scale(hazard) eform
estat ic

// Categories with relevant clincial cut offs investigated

stpm2 ib4.SBPcat, df(3) scale(hazard) eform
estat ic
stpm2 ib4.SBPcat, df(4) scale(hazard) eform
estat ic
stpm2 ib4.SBPcat, df(5) scale(hazard) eform
estat ic

stpm2 ib5.pHcategory, df(3) scale(hazard) eform
estat ic
stpm2 ib5.pHcategory, df(4) scale(hazard) eform
estat ic
stpm2 ib5.pHcategory, df(5) scale(hazard) eform
estat ic

stpm2 i.Lactatecategory, df(3) scale(hazard) eform
estat ic
stpm2 i.Lactatecategory, df(4) scale(hazard) eform
estat ic
stpm2 i.Lactatecategory, df(5) scale(hazard) eform
estat ic

stpm2 ib1.GCSbin, df(3) scale(hazard) eform
estat ic
stpm2 ib1.GCSbin, df(4) scale(hazard) eform
estat ic
stpm2 ib1.GCSbin, df(5) scale(hazard) eform
estat ic

stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio ib4.SBPcat ib5.pHcategory i.Lactatecategor ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(3) scale(hazard) eform
estat ic
stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio ib4.SBPcat ib5.pHcategory i.Lactatecategor ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(4) scale(hazard) eform
estat ic
stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio ib4.SBPcat ib5.pHcategory i.Lactatecategor ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(5) scale(hazard) eform
estat ic

// Predict mortality based on this model

stpm2 Age ib2.Gender i.FCS ib2.Med_Surg_Diag LSOA i.Readmission_within_hosp_ad PFratio ib4.SBPcat ib5.pHcategory i.Lactatecategor ib1.GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin, df(5) scale(hazard) eform

predict xb, xb
predict s, survival
predict h, hazard

foreach var in Age Gender FCS Med_Surg_Diag LSOA Readmission_within_hosp_ad PFratio GCSbin Lowest_platelet Highest_creatinine Highest_bilirubin {
  summ `var', meanonly
  local atopt `atopt' `var' `r(mean)'
   }

range timevar 0 1826.25  61

predict s0_covave, at(SBPcat 1 pHcategory 1 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s1_covave, at(SBPcat 1 pHcategory 1 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s2_covave, at(SBPcat 1 pHcategory 1 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s3_covave, at(SBPcat 1 pHcategory 1 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s4_covave, at(SBPcat 1 pHcategory 2 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s5_covave, at(SBPcat 1 pHcategory 2 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s6_covave, at(SBPcat 1 pHcategory 2 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s7_covave, at(SBPcat 1 pHcategory 2 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s8_covave, at(SBPcat 1 pHcategory 3 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s9_covave, at(SBPcat 1 pHcategory 3 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s10_covave, at(SBPcat 1 pHcategory 3 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s11_covave, at(SBPcat 1 pHcategory 3 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s12_covave, at(SBPcat 1 pHcategory 4 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s13_covave, at(SBPcat 1 pHcategory 4 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s14_covave, at(SBPcat 1 pHcategory 4 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s15_covave, at(SBPcat 1 pHcategory 4 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s16_covave, at(SBPcat 1 pHcategory 5 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s17_covave, at(SBPcat 1 pHcategory 5 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s18_covave, at(SBPcat 1 pHcategory 5 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s19_covave, at(SBPcat 1 pHcategory 5 Lactatecategor 4 `atopt') surv ci timevar(timevar)


predict s20_covave, at(SBPcat 2 pHcategory 1 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s21_covave, at(SBPcat 2 pHcategory 1 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s22_covave, at(SBPcat 2 pHcategory 1 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s23_covave, at(SBPcat 2 pHcategory 1 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s24_covave, at(SBPcat 2 pHcategory 2 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s25_covave, at(SBPcat 2 pHcategory 2 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s26_covave, at(SBPcat 2 pHcategory 2 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s27_covave, at(SBPcat 2 pHcategory 2 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s28_covave, at(SBPcat 2 pHcategory 3 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s29_covave, at(SBPcat 2 pHcategory 3 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s30_covave, at(SBPcat 2 pHcategory 3 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s31_covave, at(SBPcat 2 pHcategory 3 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s32_covave, at(SBPcat 2 pHcategory 4 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s33_covave, at(SBPcat 2 pHcategory 4 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s34_covave, at(SBPcat 2 pHcategory 4 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s35_covave, at(SBPcat 2 pHcategory 4 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s36_covave, at(SBPcat 2 pHcategory 5 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s37_covave, at(SBPcat 2 pHcategory 5 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s38_covave, at(SBPcat 2 pHcategory 5 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s39_covave, at(SBPcat 2 pHcategory 5 Lactatecategor 4 `atopt') surv ci timevar(timevar)


predict s40_covave, at(SBPcat 3 pHcategory 1 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s41_covave, at(SBPcat 3 pHcategory 1 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s42_covave, at(SBPcat 3 pHcategory 1 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s43_covave, at(SBPcat 3 pHcategory 1 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s44_covave, at(SBPcat 3 pHcategory 2 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s45_covave, at(SBPcat 3 pHcategory 2 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s46_covave, at(SBPcat 3 pHcategory 2 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s47_covave, at(SBPcat 3 pHcategory 2 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s48_covave, at(SBPcat 3 pHcategory 3 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s49_covave, at(SBPcat 3 pHcategory 3 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s50_covave, at(SBPcat 3 pHcategory 3 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s51_covave, at(SBPcat 3 pHcategory 3 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s52_covave, at(SBPcat 3 pHcategory 4 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s53_covave, at(SBPcat 3 pHcategory 4 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s54_covave, at(SBPcat 3 pHcategory 4 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s55_covave, at(SBPcat 3 pHcategory 4 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s56_covave, at(SBPcat 3 pHcategory 5 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s57_covave, at(SBPcat 3 pHcategory 5 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s58_covave, at(SBPcat 3 pHcategory 5 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s59_covave, at(SBPcat 3 pHcategory 5 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s60_covave, at(SBPcat 4 pHcategory 1 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s61_covave, at(SBPcat 4 pHcategory 1 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s62_covave, at(SBPcat 4 pHcategory 1 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s63_covave, at(SBPcat 4 pHcategory 1 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s64_covave, at(SBPcat 4 pHcategory 2 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s65_covave, at(SBPcat 4 pHcategory 2 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s66_covave, at(SBPcat 4 pHcategory 2 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s67_covave, at(SBPcat 4 pHcategory 2 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s68_covave, at(SBPcat 4 pHcategory 3 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s69_covave, at(SBPcat 4 pHcategory 3 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s70_covave, at(SBPcat 4 pHcategory 3 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s71_covave, at(SBPcat 4 pHcategory 3 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s72_covave, at(SBPcat 4 pHcategory 4 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s73_covave, at(SBPcat 4 pHcategory 4 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s74_covave, at(SBPcat 4 pHcategory 4 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s75_covave, at(SBPcat 4 pHcategory 4 Lactatecategor 4 `atopt') surv ci timevar(timevar)

predict s76_covave, at(SBPcat 4 pHcategory 5 Lactatecategor 1 `atopt') surv ci timevar(timevar)
predict s77_covave, at(SBPcat 4 pHcategory 5 Lactatecategor 2 `atopt') surv ci timevar(timevar)
predict s78_covave, at(SBPcat 4 pHcategory 5 Lactatecategor 3 `atopt') surv ci timevar(timevar)
predict s79_covave, at(SBPcat 4 pHcategory 5 Lactatecategor 4 `atopt') surv ci timevar(timevar)

